import {Navigate} from "react-router-dom"
import { getToken, removeToken } from "../utils/auth"
import {useDispatch} from "react-redux"
import { useEffect } from "react"
import { CheckLogins } from "../api/login"
import { setUser } from "../store/modules/userStore"
const AuthRouter=(props)=>{
  const dispatch=useDispatch();
    useEffect( ()=>{
        const checkLogins=async ()=>{
          try {
            const res= await CheckLogins();
            dispatch(setUser(res.data.user))
            console.log(res);
            // return res
          } catch (error) {
            removeToken();
            return
          }
        }
       checkLogins();
       
    },[dispatch])

      if(getToken()){
          return <div>{props.children}</div>
      }else{
        return  <Navigate to={"/login"}></Navigate> 
      }
}

export default AuthRouter